package ttOrder.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import ttOrder.service.IMycartService;
import ttOrder.vo.FoodVO;
@Controller
@ResponseBody
public class MycartController {
	@Autowired
	public IMycartService mycart;
	
	//点击我的购物车
	@RequestMapping(value ="/showMycart")
	public ModelAndView ShowMycart(Integer foodId,HttpSession session,ModelAndView mv){
/*		//试图从一个session中去出一个购物车
		MycartService cart = (MycartService)session.getAttribute("cart");
		if(cart==null){
			//说明该用户第一次购物
			//创建一个购物车,并放入session
			cart = new MycartService();
			
		}
		//默认购买是1盘菜,以后可以修改
		cart.addFood(foodId, "1");
	*/
		session.setAttribute("mycart", mycart);
		mv.addObject("car",mycart.showMyCart());
		mv.addObject("orderVO", mycart.getAllPrice());
		mv.setViewName("MyCart/showMycart");
		return mv;
	
	}  
	 
	//增加菜到购物车
		@RequestMapping(value="/addCarFood",method=RequestMethod.POST)
		public boolean addCarFood(Integer foodId){	
			if(mycart.getFoodsNumById(foodId) == null){
				mycart.addFood(foodId, "1");
			}else{
				int a = Integer.parseInt(mycart.getFoodsNumById(foodId))+1;
				String foodCount = String.valueOf(a);
				mycart.addFood(foodId,foodCount);
			}
			
			return true;
		}
	 
	//删除购物车的一个菜
		@RequestMapping(value="/deleteCarFood",method=RequestMethod.POST)
		public boolean deleteCarFood(Integer foodId){	
			mycart.deleteFood(foodId);		
			return true;
		}	
		
		 
	//删除购物车的全部菜
		@RequestMapping(value="/deleteAllFood",method=RequestMethod.POST)
		public boolean deleteAllFood(){	
			mycart.clear();	
			return true;
		}	
		
	//更具id返回数量
		@RequestMapping(value="/showCarFood",method=RequestMethod.POST)
		public String getFoodsNumById(Model model,Integer foodId){
			String foodCount = mycart.getFoodsNumById(foodId);
			model.addAttribute("foodCount", foodCount);
			return foodCount;
		}
		/*
		//更具id返回数量
		@RequestMapping(value="/showCarFood/{foodId}",method=RequestMethod.POST)
		public ModelAndView getFoodsNumById(ModelAndView mv,@PathVariable Integer foodId){
			mv.addObject("foodCount",mycart.getFoodsNumById(foodId));
			mv.setViewName("MyCart/showMycart");
			return mv;
		}
*/
		
	//修改菜品数量
		@RequestMapping(value="/editCarFood",method=RequestMethod.POST)
		public boolean editCarFood(@RequestParam(name = "foodId[]") Integer[] foodId,@RequestParam(name = "foodCount[]")  String[] foodCount){
			for(int i=0;i<foodId.length;i++){
				mycart.upFood(foodId[i],foodCount[i]);
			}
			return true;
		}
		
		
	//购物车提交订单(前台)
		@RequestMapping("/insertOrderAndDetall")
		public boolean insertOrderAndDetall(){
			//插入订单详情
			mycart.insertOrderDetall();
			//更改订单表
			mycart.editOrder();
			return true;
		}
}
